package com.erp.programmer.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.LocalCompanyMapper;
import com.erp.programmer.dao.RoleMapper;
import com.erp.programmer.domain.LocalCompany;
import com.erp.programmer.domain.Role;
import com.erp.programmer.dto.*;
import com.erp.programmer.service.ILocalCompanyService;
import com.erp.programmer.service.IRoleService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.erp.programmer.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-23
 */
@Service
@Transactional
public class LocalCompanyServiceImpl extends ServiceImpl<LocalCompanyMapper, LocalCompany> implements ILocalCompanyService {
    @Autowired
    LocalCompanyMapper localCompanyMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IRoleService iRoleService;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 登录验证
     * @param localCompanyDto
     * @return
     */
    @Override
    public ResponseDto<LoginUserDto> login(LocalCompanyDto localCompanyDto) {
        // 登录验证
        QueryWrapper<LocalCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",localCompanyDto.getUsername());
        LocalCompany localCompany = localCompanyMapper.selectOne(queryWrapper);
        if (localCompany==null || !localCompany.getPassword().equals(localCompanyDto.getPassword())) {
            return ResponseDto.errorByMsg(CodeMsg.USERNAME_OR_PASSWD_ERROR);
        }
        LoginUserDto loginUserDto = CopyUtil.copy(localCompany,LoginUserDto.class);
        // 发放token
        String token = UuidUtil.getShortUuid();
        loginUserDto.setToken(token);
        // 把token存入redis中 有效期1小时
        stringRedisTemplate.opsForValue().set(token, JSON.toJSONString(loginUserDto),3600, TimeUnit.SECONDS);
        return ResponseDto.success(loginUserDto);
    }

    /**
     * 获取所有员工信息
     * @return
     */
    @Override
    public ResponseDto<PageDto<LocalCompanyDto>> getAll(PageDto<LocalCompanyDto> pageDto) {
        QueryWrapper<LocalCompany> queryWrapper = new QueryWrapper<>();
        if("username".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据用户昵称搜索
            queryWrapper.like("username",pageDto.getSearchContent());
        }
        if("roleId".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据用户角色搜索
            queryWrapper.eq("role_id", pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<LocalCompany> localCompanyList = localCompanyMapper.selectList(queryWrapper);
        List<LocalCompanyDto> localCompanyListDtoList = CopyUtil.copyList(localCompanyList, LocalCompanyDto.class);

        localCompanyListDtoList.forEach(e->{
           Role roleDb = roleMapper.selectById(e.getRoleId());
           RoleDto roleDto = CopyUtil.copy(roleDb, RoleDto.class);
           e.setRoleDto(roleDto);
        });

        PageInfo<LocalCompany> pageInfo = new PageInfo<>(localCompanyList);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(localCompanyListDtoList);
        return ResponseDto.success(pageDto);
    }

    /**
     *添加员工
     * @param localCompanyDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<LocalCompanyDto>> insertLocalCompany(LocalCompanyDto localCompanyDto) {
        if (iRoleService.getById(localCompanyDto.getRoleId())==null) {
            return ResponseDto.errorByMsg(CodeMsg.ROLE_NOT_EXIST);
        }
        LocalCompany localCompany = CopyUtil.copy(localCompanyDto,LocalCompany.class);
        if (localCompanyMapper.insert(localCompany)<1) {
            return ResponseDto.errorByMsg(CodeMsg.USER_ADD_ERROR);
        }
        PageDto<LocalCompanyDto> pageDtoList = getAll(new PageDto<LocalCompanyDto>()).getData();
        ResponseDto<PageDto<LocalCompanyDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("添加成功！");
        return responseDto;
    }

    /**
     * 修改员工
     * @param localCompanyDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<LocalCompanyDto>> updateLocalCompany(LocalCompanyDto localCompanyDto) {
        if (iRoleService.getById(localCompanyDto.getRoleId())==null) {
            return ResponseDto.errorByMsg(CodeMsg.USER_ROLE_EMPTY);
        }
        LocalCompany localCompany = CopyUtil.copy(localCompanyDto, LocalCompany.class);
        if (localCompanyMapper.updateById(localCompany)<1) {
            return ResponseDto.errorByMsg(CodeMsg.USER_EDIT_ERROR);
        }
        PageDto<LocalCompanyDto> pageDtoList = getAll(new PageDto<LocalCompanyDto>()).getData();
        ResponseDto<PageDto<LocalCompanyDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("修改成功！");
        return responseDto;
    }


}
